package com.zhl.stream7;


import java.util.*;
import java.util.stream.Collectors;

public class StreamTest4 {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        Student s1 = new Student(25,176.5,"蜘蛛精");
        Student s2 = new Student(25,176.5,"蜘蛛精");
        Student s3 = new Student(20,160.0,"紫霞仙子");
        Student s4 = new Student(52,170.0,"白晶晶");
        Student s5 = new Student(23,180.0,"白雪公主");
        Student s6 = new Student(34,185.0,"牛夫人");
        Collections.addAll(students,s1,s2,s3,s4,s5,s6);

        //1.需求1：计算身高超过168的人数
        long size = students.stream().filter(s -> s.getHeight() > 168).count();
        System.out.println("身高超过168的人数：" + size);

        //2.需求2：找出身高最高的学生，并输出
        Student s = students.stream().max((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
        System.out.println(s);

        //3.需求3：找出身高最矮的学生，并输出
        Student stu1 = students.stream().min((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
        System.out.println(stu1);

        //4.找出身高超过170的学生对象，并放到一个新集合中去返回
        //流只能收集一次
        List<Student> students1 = students.stream().filter(o -> o.getHeight() > 170).collect(Collectors.toList());
        System.out.println(students1);

        Set<Student> students2 = students.stream().filter(o -> o.getHeight() > 170).distinct().collect(Collectors.toSet());
        System.out.println(students2);

        //5.找出身高超过170的学生对象，并把学生对象的名字和身高，存入到一个Map集合返回
        Map<String, Double> map = students.stream().filter(o -> o.getHeight() > 170).distinct().collect(Collectors.toMap(a -> a.getName(), a -> a.getHeight()));
        System.out.println(map);

        Object[] arr = students.stream().filter(o -> o.getHeight() > 170).distinct().toArray();
        System.out.println(Arrays.toString(arr));


    }

}
